﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1
{
    public partial class Dispatch : Form
    {
        public Dispatch()
        {
            InitializeComponent();
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                String getMax = "SELECT ISNULL(MAX(Recno),0) FROM dispatch";
                SqlDataReader mmdr = DbConnet.ReadTable(getMax);
                String mx = "";

                if (mmdr.Read())
                {
                    mx = mmdr[0].ToString();
                }
                mmdr.Close();

                //mx = mx.Substring(mx.IndexOf("DI") + 1);

                int a = (int.Parse(mx) + 1);

                txtDisCode.Text =  a.ToString();
            }
            catch (Exception exietm)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Boolean upd = false;
            Boolean ins = false;
            int inhnd = 0;

            try
            {
                String item = txtItem.Text.Trim();
                item = item.Substring(0, item.IndexOf(" - "));
                String updDis = "UPDATE dispatch SET noofitems = '" + txtNoOfItems.Text.Trim() + "',updatedby = '" + new Midval().getUser() + "' ,updateteddate = '" + DateTime.Now + "' WHERE recno = '" + txtDisCode.Text + "' AND datex LIKE '%" + dtdisDate.Value.ToString("yyyy-MM-dd") + "%' ";
                String insDis = " INSERT INTO dispatch (Recno,datex,item,noofitems,createdby,createddate) " +
                                " VALUES ('"+txtDisCode.Text.Trim()+"','"+dtdisDate.Value+"','"+item+"','"+txtNoOfItems.Text+"','"+new Midval().getUser()+"','"+DateTime.Now+"')";

                upd = DbConnet.AddEditDel(updDis);

                if (!upd)
                    ins = DbConnet.AddEditDel(insDis);

                if (upd || ins)
                {
                    
                    String getC = "SELECT  ISNULL(noofitems,0) FROM stock WHERE Itemcode = '" + item + "'";
                    SqlDataReader cdr = DbConnet.ReadTable(getC);

                    if (cdr.Read())
                    {
                        inhnd = int.Parse(cdr[0].ToString());
                    }
                    cdr.Close();

                    int dis = 0;

                    try
                    {
                        dis = int.Parse(txtNoOfItems.Text.Trim());
                    }
                    catch
                    {
                        dis = 0;
                    }

                    String updStk = "UPDATE stock SET noofitems ='" + (inhnd - (dis)) + "',lastupdateted = '" + DateTime.Now + "' WHERE Itemcode = '" + item + "'";
                    Boolean upst = DbConnet.AddEditDel(updStk);

                    if (upst)
                    {

                        MessageBox.Show(new Form() { TopMost = true }, "Dispatch Details Added Update Successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtDisCode.Clear();
                        txtItem.Clear();
                        txtNoOfItems.Clear();
                    }
                }

                String getRo = "SELECT ISNULL(reorder,0) FROM Items WHERE itemcode = '" + item + "' ";
                SqlDataReader rodr = DbConnet.ReadTable(getRo);
                int ro = 0;

                if (rodr.Read())
                {
                    ro  = int.Parse(rodr[0].ToString());
                }
                rodr.Close();

                if (inhnd <= ro)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "You are low on "+txtItem.Text+", You have "+inhnd+" no of items, Please Reoder. ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception exsub)
            {
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            txtDisCode.Clear();
            txtItem.Clear();
            txtNoOfItems.Clear();
        }

        private void txtItem_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            new Item_Popup().ShowDialog();
            txtItem.Text = new Midval().getItemName();
        }

        private void Dispatch_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
        }

        private void txtDisCode_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                new Midval().setDisDate(dtdisDate.Value.ToString("yyyy-MM-dd"));
                new dispatch_popup().ShowDialog();
                String val = new Midval().getDisCode();
                val = val.Substring(0, val.IndexOf(" - "));
                txtDisCode.Text = val;
            }
            catch
            {
            }
        }

        private void txtDisCode_TextChanged(object sender, EventArgs e)
        {
            try
            {

                String getDis = "SELECT Recno,item,noofitems FROM  dispatch WHERE Recno = '" + txtDisCode.Text.Trim() + "' AND datex BETWEEN '" + dtdisDate.Value.ToString("yyyy-MM-dd") + "' AND '" + dtdisDate.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
                SqlDataReader disdr = DbConnet.ReadTable(getDis);
                String item = "";
                while (disdr.Read())
                {
                    txtDisCode.Text = disdr["Recno"].ToString();
                    item = disdr["item"].ToString();
                    txtNoOfItems.Text = disdr["noofitems"].ToString();
                }
                disdr.Close();

                String getName = "SELECT itemname FROM Items WHERE Itemcode = '" + item + "'";
                SqlDataReader ndr = DbConnet.ReadTable(getName);
                if (ndr.Read())
                {
                    txtItem.Text = item + " - " + ndr["itemname"].ToString();
                }
                ndr.Close();

            }
            catch (Exception exdis)
            {
            }
        }
    }
}
